package com.atguigu.tingshu.user.pattern.impl;

import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.pattern.UserPaidStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: atguigu
 * @create: 2024-03-09 11:35
 */
@Slf4j
@Component(SystemConstant.ORDER_ITEM_TYPE_VIP)
public class VIPUserPaidStrategy implements UserPaidStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    /**
     * 处理购买项目类型为：VIP
     *
     * @param userPaidRecordVo
     */
    @Override
    public void saveUserPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("处理购买项目类型为：VIP,{}", userPaidRecordVo);
        //3.处理会员购买记录
        //3.1 为购买会员保存会员购买记录
        //3.1.1 根据订单编号查询已购会员记录是否存在
        LambdaQueryWrapper<UserVipService> userVipServiceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userVipServiceLambdaQueryWrapper.eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo());
        userVipServiceLambdaQueryWrapper.last("limit 1");
        Long count = userVipServiceMapper.selectCount(userVipServiceLambdaQueryWrapper);
        if (count > 0) {
            return;
        }
        //3.1.2 根据购买套餐ID查询套餐信息-得到会员服务月数用于计算过期时间
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        Integer serviceMonth = vipServiceConfig.getServiceMonth();

        //3.1.3 判断用户会员新开或者会员续费
        UserVipService userVipService = new UserVipService();
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        int isVip = userInfo.getIsVip().intValue();
        if (isVip == 1 && userInfo.getVipExpireTime().after(new Date())) {
            //会员续费 本次VIP购买生效时间是现有会员过期时间
            userVipService.setStartTime(userInfo.getVipExpireTime());
            userVipService.setExpireTime(DateUtil.offsetMonth(userInfo.getVipExpireTime(), serviceMonth));
        } else {
            //新开会员 本次VIP购买生效时间为当前时间
            userVipService.setStartTime(new Date());
            userVipService.setExpireTime(DateUtil.offsetMonth(new Date(), serviceMonth));
        }
        //3.1.4 新增会员购买记录
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipServiceMapper.insert(userVipService);

        //3.2 更新用户会员标识以及延长会员过期时间
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
